package com.liaoyifan.core.valid.validator;

import com.liaoyifan.core.valid.constraints.Scale;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.math.BigDecimal;

@SuppressWarnings("unused")
public class ScaleValidator implements ConstraintValidator<Scale, BigDecimal> {

    private int value;

    @Override
    public void initialize(Scale constraintAnnotation) {
        this.value = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(BigDecimal value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        return value.scale() <= this.value;
    }
}
